####################################################################################
## 关键驱动基因鉴定
## IM->IGC以及IM->DGC分开来描述
## 1、关注已报道驱动基因和我们鉴定的驱动基因
## 2、在多少人中是肠化和胃癌共享
## 3、在多少共享的人中肿瘤细胞比例超过0.6
## 若存在共享，至少满足其中50%受到进化选择(排除其可能是突变率较高，偶然出现在强势克隆的克隆簇中)

clone_t=0.6
choose_rate=0.5
cancer_type=( "IGC" "DGC" )
for Type in ${cancer_type[@]}
do
echo $Type
${Rscript} ${scripts_path}/tree/JudgeGeneDriverSubtype.R \
--muti_cancer ${maf_path}/All_GGA.cancer.maf \
--muti_pre ${maf_path}/All_GGA.precancer.maf \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--gene_list ${work_dir}/public_ref/importTantGene.list \
--sample_info ${config_path}/tumor_normal.class.list \
--type ${Type} \
--clone_t ${clone_t} \
--choose_rate ${choose_rate} \
--out_path ${Images_path}/ITH

## 展示关键驱动基因的CCF改变
${Rscript} ${scripts_path}/plot/mutCCF_plot.chooseGene.bar.R \
--gene_list_file ${Images_path}/ITH/Driver_Trunk.evolutionChoose.${Type}.tsv \
--info_file ${config_path}/STAD-useCombine.Sample.tsv \
--type ${Type} \
--class_sub_file ${config_path}/Class_order_sub.list \
--lollipop_file ${Images_path}/lollipop/${gene}/${gene}.AllInfo.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/ITH
done


####################################################################################
## 20230301
## pyclone + citeup
## 亚克隆分析
## 包含pyclone及突变质控
sh ${scripts_path}/module/computePyclone.CiteUp.sh

###################################################
## 5、citeup的cluster的画图
## 87个样本成功构建进化树（共98个）
success_sample=`ls ${Pyclone_path}/Citeup_subClone_result/ | grep h5$  | awk -F'_Citeup' '{print $1}' | tr "\n" '|'  | sed 's/|$//'`
cat ${config_path}/tumor_normal.list ${config_path}/tumor_normal.MSI.list | grep -E -w ${success_sample} | grep -v Normal | awk -F, '{print $2}' | sort -u | xargs -i -P 6 sh -c '
${citup_rscript} ${scripts_path}/pyclone/Pyclone_citupPlot.R \
--work_dir ${work_dir} \
--citup_file ${Pyclone_path}/Citeup_subClone_result/{}_Citeup.h5 \
--pyclone_file ${Pyclone_path}/result/CiteUp/{}/tables/loci.tsv \
--smg_gene_file ${work_dir}/public_ref/importTantGene.list \
--citup_input_path ${Pyclone_path}/Citeup_subClone \
--sample {} \
--output_path ${Pyclone_path}/Citeup_subClone_plot \
--class_order ${config_path}/Class_order.list \
--class_a_order ${config_path}/Class_order_sub.list \
--sample_file ${config_path}/tumor_normal.class.MSS_MSI.list 
'

## 构建成功的样本
## 89个人 = 77MSS + 10MSI
mkdir -p ${Pyclone_path}/selectGCClone
cat ${config_path}/tumor_normal.class.list | grep -E -w "Normal|${success_sample}" > ${Pyclone_path}/selectGCClone/tumor_normal.class.pyclone.list
cat ${config_path}/tumor_normal.class.MSI.list | grep -E -w "Normal|${success_sample}" > ${Pyclone_path}/selectGCClone/tumor_normal.class.pyclone.MSI.list

####################################################################################
## 20230314
## 鉴定促进癌变的关键基因
## 1、共72个MSS稳定的患者（总共81人），成功构建了克隆演化树；
## 2、基于克隆演化鉴定，促进IM癌变的基因的标准：
## 	A、至少1个人的IM和GC共享且在GC中CCF>=0.6
## 	B、去除肠化中突变率显著高于胃癌的基因
## 	C、以下条件满足任一则纳入
## 		1、已报道的胃癌的SMG或COMISC鉴定的癌或抑癌基因
##		2、非已报道，满足以下任一
##			A、至少1例突变所在主克隆簇不和已报道基因一起出现（X个基因）
##			B、在其它同一类型至少2例肿瘤样本中存在突变CCF>=0.6（X个基因）
cat ${work_dir}/public_ref/importTantGene.list > ${work_dir}/public_ref/importTantGene.addCGC.list
cat ${work_dir}/public_ref/cancer_gene_census.list | grep -E "oncogene|TSG"  | awk -F'\t' '{print $1}' >> ${work_dir}/public_ref/importTantGene.addCGC.list

${citup_rscript} ${scripts_path}/pyclone/Pyclone_getGastricCloneGene.R \
--work_dir ${work_dir} \
--pyclone_path ${Pyclone_path}/CloneImages \
--smg_gene_file ${work_dir}/public_ref/importTantGene.addCGC.list \
--output_path ${Pyclone_path}/selectGCClone \
--clone_t 0.6 \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--class_order ${config_path}/Class_order.list \
--class_a_order ${config_path}/Class_order_sub.list \
--sample_file ${Pyclone_path}/selectGCClone/tumor_normal.class.pyclone.list \
--sample_msi_file ${Pyclone_path}/selectGCClone/tumor_normal.class.pyclone.MSI.list

## 记录每一步质控的基因
## GCClone_gene.reord.tsv
## 基因每个样本的突变情况以及所在cluster是否存在SMG
## GCClone_gene.tsv
####################################################################################
## 计算关注基因的突变率
## 去除IM中突变率显著高于GC的样本
${Rscript} ${scripts_path}/pyclone/Pyclone_geneMutRateFilter.R \
--mut_rate_gene_file ${Images_path}/mutRate/MutRate.tsv \
--info_file ${work_dir}/baseTable/STAD_Info.addBurden.MSI_MSS.addCNVType.tsv \
--gene_file ${Pyclone_path}/selectGCClone/GCClone_gene.reord.tsv \
--smg_file ${work_dir}/public_ref/importTantGene.list \
--cgc_file ${work_dir}/public_ref/cancer_gene_census.list \
--images_path ${Pyclone_path}/selectGCClone 
## 记录每一步质控的基因,同时输出突变率
## GCClone_gene.reord.addMutRate.tsv

####################################################################################
## 克隆演化鉴定的关键基因(原始)
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | awk -F'\t' '{if($16 == "TRUE")print $1}' \
> ${Pyclone_path}/selectGCClone/GCCloneGene.use.list

echo Gene_Symbol > ${Pyclone_path}/selectGCClone/GCClone_gene.IGC_DGC.list
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | grep -w "IGC_DGC" | \
awk -F'\t' '{if($16 == "TRUE")print $1}' \
>> ${Pyclone_path}/selectGCClone/GCClone_gene.IGC_DGC.list 

## IGC进化的基因
echo Gene_Symbol > ${Pyclone_path}/selectGCClone/GCClone_gene.IGC.list
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | grep -w "IGC" | \
awk -F'\t' '{if($16 == "TRUE")print $1}' \
>> ${Pyclone_path}/selectGCClone/GCClone_gene.IGC.list 

## DGC进化选择的基因
echo Gene_Symbol > ${Pyclone_path}/selectGCClone/GCClone_gene.DGC.list
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | grep -w "DGC" | \
awk -F'\t' '{if($16 == "TRUE")print $1}' \
>> ${Pyclone_path}/selectGCClone/GCClone_gene.DGC.list 

## 经过人工质控
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.ManualCheck.tsv | sed '1d' | awk -F'\t' '{print $1}' \
> ${Pyclone_path}/selectGCClone/GCCloneGene.use.list

## IGC和DGC共享进化的基因
echo Gene_Symbol > ${Pyclone_path}/selectGCClone/GCClone_gene.IGC_DGC.list
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.ManualCheck.tsv | \
awk -F'\t' '{if($6 == "IGC_DGC")print $1}' \
>> ${Pyclone_path}/selectGCClone/GCClone_gene.IGC_DGC.list 

## IGC进化的基因
echo Gene_Symbol > ${Pyclone_path}/selectGCClone/GCClone_gene.IGC.list
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.ManualCheck.tsv | \
awk -F'\t' '{if($6 == "IGC")print $1}' \
>> ${Pyclone_path}/selectGCClone/GCClone_gene.IGC.list 

## IGC进化选择的基因
echo Gene_Symbol > ${Pyclone_path}/selectGCClone/GCClone_gene.DGC.list
cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.ManualCheck.tsv | \
awk -F'\t' '{if($6 == "DGC")print $1}' \
>> ${Pyclone_path}/selectGCClone/GCClone_gene.DGC.list 

## 克隆演化鉴定的关键基因 + 新鉴定的驱动基因
echo Gene_Symbol > ${work_dir}/public_ref/importTantGene.addGCClone.list
cat ${mutsig_check_path}/smg.list ${Pyclone_path}/selectGCClone/GCCloneGene.use.list | sort -u | grep -v Gene_Symbol \
>> ${work_dir}/public_ref/importTantGene.addGCClone.list

success_sample=`ls ${Pyclone_path}/Citeup_subClone_result/ | grep h5$  | awk -F'_Citeup' '{print $1}' | tr "\n" '|'  | sed 's/|$//'`
cat ${config_path}/tumor_normal.list | grep -E -w ${success_sample} | grep -v Normal | awk -F, '{print $2}' | sort -u | xargs -i -P 6 sh -c '
${citup_rscript} ${scripts_path}/pyclone/Pyclone_citupPlot.R \
--work_dir ${work_dir} \
--citup_file ${Pyclone_path}/Citeup_subClone_result/{}_Citeup.h5 \
--pyclone_file ${Pyclone_path}/result/CiteUp/{}/tables/loci.tsv \
--smg_gene_file ${work_dir}/public_ref/importTantGene.addGCClone.list \
--citup_input_path ${Pyclone_path}/Citeup_subClone \
--sample {} \
--output_path ${Pyclone_path}/Citeup_subClone_plot_addGCClone \
--class_order ${config_path}/Class_order.list \
--class_a_order ${config_path}/Class_order_sub.list \
--sample_file ${config_path}/tumor_normal.class.MSS_MSI.list 
'

## 描述每个样本的driver基因VAF情况以及对应样本的突变
## 新的样本列表
${Rscript} ${scripts_path}/tree/GetDriverEverySample.R \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--ccf_msi_file ${MutationTime_path}/result/All_CCF_mutTime.MSI.tsv \
--gene_list ${work_dir}/public_ref/importTantGene.addGCClone.list  \
--sample_info ${config_path}/tumor_normal.class.MSS_MSI.list \
--out_path ${Images_path}/DriverEverySample \
--class_order_file ${config_path}/Class_order_sub.list 

## 将所有鉴定的进化选择基因画图
## IGC和DGC共享的基因
## 画所有感兴趣的基因集合在最后使用的样本中的共享情况
type_list=("IGC_DGC" "IGC" "DGC")
clone_t=0.6
choose_rate=0
for Type in ${type_list[@]}
do
echo $Type
${Rscript} ${scripts_path}/tree/JudgeGeneDriverSubtype.R \
--muti_cancer ${maf_path}/All_GGA.cancer.maf \
--muti_pre ${maf_path}/All_GGA.precancer.maf \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--gene_list ${Pyclone_path}/selectGCClone/GCClone_gene.${Type}.list  \
--sample_info ${Pyclone_path}/selectGCClone/tumor_normal.class.pyclone.list \
--type ${Type} \
--clone_t ${clone_t} \
--choose_rate ${choose_rate} \
--out_path ${Pyclone_path}/selectGCClone/${Type}
done


## 五个IGC和DGC都有的样本
## 展示所有的错义突变
cat ${config_path}/tumor_normal.class.list | grep -E -w ${success_sample} | grep "IM + IGC + DGC" | grep -v Normal | awk '{print $2}' | sort -u | xargs -i -P 6 sh -c '
${citup_rscript} ${scripts_path}/pyclone/Pyclone_citupPlot.IGC_DGC.R \
--work_dir ${work_dir} \
--citup_file ${Pyclone_path}/Citeup_subClone_result/{}_Citeup.h5 \
--pyclone_file ${Pyclone_path}/result/CiteUp/{}/tables/loci.tsv \
--smg_gene_file ${work_dir}/public_ref/importTantGene.addGCClone.list \
--citup_input_path ${Pyclone_path}/Citeup_subClone \
--sample {} \
--output_path ${Pyclone_path}/Citeup_subClone_plot_addGCClone_IGC-DGC \
--class_order ${config_path}/Class_order.list \
--class_a_order ${config_path}/Class_order_sub.list \
--sample_file ${config_path}/tumor_normal.class.MSS_MSI.list 
'

## 每个基因分别链接对应的样本所在克隆演化图
rm -rf ${Pyclone_path}/Citeup_subClone_plot_DriverGene
mkdir -p ${Pyclone_path}/Citeup_subClone_plot_DriverGene
for gene in `cat ${Pyclone_path}/selectGCClone/GCCloneGene.use.list`
do
## 判断样本为DGC还是IGC
out_type=`cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | grep -w $gene | awk -F'\t' '{print $7}'`

mkdir -p ${Pyclone_path}/Citeup_subClone_plot_DriverGene/${out_type}/${gene}
for sample in `cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | grep -w $gene | awk -F'\t' '{print $9}' | tr ',' '\n'`
do
ln -nsf ${Pyclone_path}/Citeup_subClone_plot_addGCClone/${sample}_plot_combine.pdf ${Pyclone_path}/Citeup_subClone_plot_DriverGene/${out_type}/${gene}
id=`cat ${baseTable_path}/STAD_Info.addBurden.MSI_MSS.addCNVType.tsv | grep -w ${sample} | awk -F'\t' '{print $2}' | sort -u`
## 链接进化树
pdf_file=`find ${tree_path}/TreeClass_Revise | grep -v check | grep ${id}_mlhtree.pdf`
ln -snf ${pdf_file} ${Pyclone_path}/Citeup_subClone_plot_DriverGene/${out_type}/${gene}/${sample}_mlhtree.pdf
## 链接驱动基因的CCF
ln -snf ${Images_path}/DriverEverySample/${id}_Driver.pdf ${Pyclone_path}/Citeup_subClone_plot_DriverGene/${out_type}/${gene}/${sample}_Driver.pdf
done
done

####################################################################################
## 20230315
## 进一步确定可靠的基因集合
## 每个基因，其所在的所有进化树，所有基因的CCF改变，所在样本的克隆演化树
Variant_Types="Missense_Mutation|Nonsense_Mutation|Frame_Shift_Ins|Frame_Shift_Del|In_Frame_Ins|In_Frame_Del|Splice_Site|Nonstop_Mutation"

rm -rf ${Pyclone_path}/ChooseGene_Info
mkdir -p ${Pyclone_path}/ChooseGene_Info
for gene in `cat ${Pyclone_path}/selectGCClone/GCCloneGene.use.list`
do
## 判断IGC和DGC应该是自己进行判断
mkdir -p ${Pyclone_path}/ChooseGene_Info/${gene}
## 在IM中存在且为Trunk的基因
for sample in `cat ${Pyclone_path}/selectGCClone/GCClone_gene.reord.addMutRate.tsv | grep -w $gene | awk -F'\t' '{print $9}' | tr ',' '\n'`
do
ln -nsf ${Pyclone_path}/Citeup_subClone_plot_addGCClone/${sample}_plot_combine.pdf ${Pyclone_path}/ChooseGene_Info/${gene}
done
## 存在突变的样本
for sample in `cat ${MutationTime_path}/result/All_CCF_mutTime.tsv | grep -w ${gene} | grep -E -w ${Variant_Types} | awk -F'\t' '{print $1}' | sort -u`
do
Normal=`cat ${config_path}/tumor_normal.class.list | grep -w ${sample} | awk -F'\t' '{print $2}' | sort -u`
id=`cat ${config_path}/tumor_normal.class.list | grep -w ${sample} | awk -F'\t' '{print $1}' | sort -u`
## 链接进化树
ln -snf ${tree_path}/Tree_file/${Normal}_mlhtree.pdf ${Pyclone_path}/ChooseGene_Info/${gene}/
ln -snf ${tree_path}/Tree_file/${Normal}_variants.csv ${Pyclone_path}/ChooseGene_Info/${gene}/
## 链接驱动基因的CCF
ln -snf ${Images_path}/DriverEverySample/${id}_Driver.pdf ${Pyclone_path}/ChooseGene_Info/${gene}/${Normal}_Driver.pdf
done
done

####################################################################################
## 9个基因的突变图谱
class_type_list=("IGC" "DGC" "IGC_DGC")
for class_type in ${class_type_list[@]}
do
${Rscript} ${scripts_path}/plot/waterfull_smg.SortBySample.R \
--type ${class_type} \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.addShare.tsv \
--images_path ${Images_path} \
--info_file ${config_path}/tumor_normal.class.list \
--class_order_file ${config_path}/Class_order.list \
--class_order_sub_file ${config_path}/Class_order_sub.list \
--igc_gene_list ${Pyclone_path}/selectGCClone/GCClone_gene.IGC.list \
--dgc_gene_list ${Pyclone_path}/selectGCClone/GCClone_gene.DGC.list \
--all_gene_list ${Pyclone_path}/selectGCClone/GCClone_gene.IGC_DGC.list \
--tp53_pre_file ${Images_path}/lollipop/TP53_NMU/TP53.PreCancerous.UniqueNormal.tsv \
--tp53_cancer_file ${Images_path}/lollipop/TP53_NMU/TP53.Cancerous.UniqueNormal.tsv \
--apc_pre_file ${Images_path}/lollipop/APC_NMU/APC.PreCancerous.UniqueNormal.tsv \
--apc_cancer_file ${Images_path}/lollipop/APC_NMU/APC.Cancerous.UniqueNormal.tsv 
done

####################################################################################
## 肿瘤异质性的计算
## 排除IGC和DGC均有的样本
## 计算IM_IGC和IM_DGC的瘤内异质性
${Rscript} ${scripts_path}/tree/ComputeHeterogeneity.OnlyHeterogeneity.R \
--muti_cancer ${maf_path}/All_GGA.cancer.maf \
--muti_pre ${maf_path}/All_GGA.precancer.maf \
--gene_list ${Pyclone_path}/selectGCClone/GCCloneGene.use.list \
--sample_info ${config_path}/tumor_normal.class.list \
--out_path ${Images_path}/ITH_CloneChoose

## 以共享驱动突变定义两种亚型，判断瘤内异质性
${Rscript} ${scripts_path}/tree/ComputeHeterogeneity.TrunkDriver.R \
--muti_cancer ${maf_path}/All_GGA.cancer.maf \
--muti_pre ${maf_path}/All_GGA.precancer.maf \
--gene_list ${Pyclone_path}/selectGCClone/GCCloneGene.use.list \
--preGene_list ${work_dir}/mutsig_check/im_smg.list \
--ith_file ${Images_path}/ITH_CloneChoose/ITH.compute.uniqueNormal.tsv \
--ith_sample_file ${Images_path}/ITH_CloneChoose/ITH.compute.allSample.tsv \
--sample_info ${config_path}/tumor_normal.class.list \
--out_path ${Images_path}/ITH_CloneChoose
